package controller;

import view.RecordView;
import model.Record;
import controller.eventListeners.CreateRecordTextFieldFocusListener;

public class CreateRecordController {
	
	private RecordView recordView;
	private static CreateRecordController instance;
	private CreateRecordTextFieldFocusListener focusListener;
	private Record record;
	
	
	private CreateRecordController() {
		
	}
	
	public static CreateRecordController getInstance() {
		if (instance == null){
			instance = new CreateRecordController();
			
		}
		return instance;
	}
	
	private RecordView getRecordView() {
		recordView = new RecordView();
		record = new Record();
		addListeners();
		return recordView;
	}
	
	public void showRecordView() {
		getRecordView().setVisible(true);
	}
	
	private void addListeners() {
		focusListener = new CreateRecordTextFieldFocusListener();
		recordView.getInText().addFocusListener(focusListener);
		recordView.getTelstraClearText().addFocusListener(focusListener);
		recordView.getVodafoneText().addFocusListener(focusListener);
		recordView.getGroceryText().addFocusListener(focusListener);
		recordView.getInternetText().addFocusListener(focusListener);
		recordView.getElectricityText().addFocusListener(focusListener);
		recordView.getMiscText().addFocusListener(focusListener);
		recordView.getCreditCardText().addFocusListener(focusListener);
		recordView.getNationalBankText().addFocusListener(focusListener);
	}
	
	public Record getRecord() {
		return record;
	}
	
	public void setRecord(Record record) {
		this.record = record;
	}
	
	public void updateValues() {
		record = recordView.getRecord();
		recordView.getSavedLabel().setText(toCurrency(record.getSaved()));
		recordView.getTotalExpensesLabel().setText(toCurrency(record.getTotalExpenses()));
		recordView.getTotalLabel().setText("$" + toCurrency(record.getTotalBills()));
	}
	
	public String toCurrency(int value) {
		double v= (double)value / 100;
		return Double.toString(v);
	}
}
